package com.jtech.together.mvp.presenter;

import android.content.Context;
import android.os.Bundle;

import com.jtech.together.manager.UserManager;
import com.jtech.together.model.FriendModel;
import com.jtech.together.model.event.AcceptEvent;
import com.jtech.together.mvp.contract.MessageContract;
import com.jtech.together.util.BmobUtil;
import com.jtech.together.util.Bus;
import com.jtech.together.util.IM;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.newim.listener.MessageSendListener;
import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListListener;

/**
 * 消息页面
 * Created by wuxubaiyang on 2017/5/10.
 */
public class MessagePresenter implements MessageContract.Presenter {
    private Context context;
    private MessageContract.View view;

    public MessagePresenter(Context context, MessageContract.View view, Bundle bundle) {
        this.context = context;
        this.view = view;
    }

    @Override
    public void loadAllConversation() {
        List<BmobIMConversation> conversations = new ArrayList<>();
        //移除掉没有消息的会话
        List<BmobIMConversation> tempList = BmobIM.getInstance()
                .loadAllConversation();
        if (null != tempList) {
            for (BmobIMConversation conversation : tempList) {
                if (null != conversation.getMessages() && conversation.getMessages().size() > 0) {
                    conversations.add(conversation);
                } else {
                    BmobIM.getInstance().deleteConversation(conversation);
                }
            }
        }
        view.loadSuccess(conversations);
    }

    @Override
    public void acceptAddFriend(final BmobIMConversation conversation, final BmobIMUserInfo userInfo, final String message) {
        List<BmobObject> friendModels = new ArrayList<>();
        String userId = UserManager.getCurrentUserId();
        String friendId = userInfo.getUserId();
        friendModels.add(new FriendModel(userId, friendId));
        friendModels.add(new FriendModel(friendId, userId));
        new BmobBatch()
                .insertBatch(friendModels)
                .doBatch(new QueryListListener<BatchResult>() {
                    @Override
                    public void done(List<BatchResult> o, BmobException e) {
                        if (e == null) {
                            sendAgreeAddFriendMessage(conversation, message);
                            //发送接受好友请求的事件
                            Bus.get().post(new AcceptEvent(userInfo.getUserId()));
                        } else {
                            view.operateFail(BmobUtil.getErrorMessage(e.getErrorCode()));
                        }
                    }
                });
    }

    /**
     * 发送聊天消息
     *
     * @param conversation
     * @param message
     */
    private void sendAgreeAddFriendMessage(BmobIMConversation conversation, String message) {
        IM.build(conversation)
                .agreeAddFriendMessage(message, new MessageSendListener() {
                    @Override
                    public void done(BmobIMMessage bmobIMMessage, BmobException e) {
                        if (null == e) {
                            view.operateSuccess();
                        } else {
                            view.operateFail(BmobUtil.getErrorMessage(e.getErrorCode()));
                        }
                    }
                });
    }
}